package pub.tbc.atps.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import pub.tbc.toolkit.CastUtil;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author tbc on 2016/11/14 20:43:41.
 */
@Data
@Slf4j
public class PropConfig {

    private static final String FILE_POSTFIX = ".properties";
    private static final String DEFAULT_CONFIG_FILE = "config";

    //    private Integer threadCount;
    public static Properties load() {
        return load(DEFAULT_CONFIG_FILE);
    }

    /**
     * 加载属性文件
     *
     * @param filename
     * @return
     * @author tbc
     * @version 1.0 {2016年6月29日 下午5:07:34}
     */
    public static Properties load(String filename) {
        filename = filename + FILE_POSTFIX;
        Properties props = null;
        try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename)) {
            if (is == null) {
                throw new FileNotFoundException(filename + " file is not found");
            }
            props = new Properties();
            props.load(is);
        } catch (Exception e) {
            log.error("load properties file failure ", e);
        }
        return props;
    }

    public static String get(Properties properties, String key) {
        return properties.getProperty(key);
    }

    public static String get(String fileName, String key) {
        return get(load(fileName), key);
    }

    public static int getInt(String fileName, String key) {
        return CastUtil.castInt(get(fileName, key));
    }

    public static long getLong(String fileName, String key) {
        return CastUtil.castLong(get(fileName, key));
    }

    public static double getDouble(String fileName, String key) {
        return CastUtil.castDouble(get(fileName, key));
    }

    public static String get(String key) {
        return get(load(), key);
    }

    public static int getInt(String key) {
        return CastUtil.castInt(get(key));
    }

    public static long getLong(String key) {
        return CastUtil.castLong(get(key));
    }

    public static double getDouble(String key) {
        return CastUtil.castDouble(get(key));
    }


}
